import httpRequest from './httpRequest'
import cache from './cache'
/**
 * 全局可用方法，直接返回请求结果
 */
export default {
	/**
	 * 获取字典
	 * 调用示例：方法前加async 请求方法前加await
	 * async invock(){
	 * var obj =await this.getDicByDictCodes()
	 * }
	 */
     async getDicByDictCodes(dictCodes){
		var result
		var _this = this 
		await httpRequest({
		    url: httpRequest.adornUrl(`/caac/oa/comm/dict/dicts`),
		    method: 'get',
		    params: httpRequest.adornParams({
			    'dictCodes': dictCodes,
			    'systemKey': 'zhangqi',//固定值
			    'systemFlag': 'yidong'//固定值
		    })
		}).then(({data}) => {
			result = data
			if(data.code==500){
				console.log("获取字典出错"+data.message);
			}
			
		})
		// console.log("result1: " + JSON.stringify(result));
	    return result
	},
	/**
	 * 根据字典数组和字典code获取字典名称
	 * @param {Object} dictArr 字典数组
	 * @param {Object} dicItemCode 字典code
	 */
	getDictItemName(dictArr,dicItemCode){
		console.log("dicItemCode--->: " + dicItemCode);
	    if (!dictArr) return
	    for (let item of dictArr) {
			console.log("工具----》》item: " + JSON.stringify(item));
		  if (item.dictItemCode == dicItemCode) {
				return item.dictItemName
			}
		}
	},
	
	/**
	 * 根据字典类型和字典code获取字典名称
	 * @param {Object} dicItemType 字典类型
	 * @param {Object} dicItemCode 字典code
	 * 调用示例  this.dict.getDictNameByDictCode(dicItemType,dicItemCode)
	 */
	getDictNameByDictCode(dicItemType,dicItemCode){
		let dictArr = cache.get(dicItemType)
		if(!dictArr) return
		for (let item of dictArr) {
		  if (item.dictItemCode == dicItemCode) {
				return item.dictItemName
			}
		}
		
	},
	//生成年份，供下拉选择
	getYearArr () {
	    var date = new Date();
	    var y = date.getFullYear();
	    var arraySize = 12;
	    var yearArray = new Array();
	    for (var i = 0; i < arraySize; i++) {
			yearArray.push({'name':y+i-arraySize+2})
	        // yearArray[i] = y+i-arraySize+2;
	    }
	    return yearArray;
	},
	//获取当前年YYYY
	getCurrentYear(){
		var date = new Date();
		return date.getFullYear();
	}
}
